<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@page import="connection.ConnectionWrapper"%>
<%@page import="model.News"%>
<%@ page language="java" import="java.sql.*,java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- includes -->
<%@ include file="includes/includes.jsp"%>
<!-- fin includes -->
<title>Insert title here</title>
</head>
<body>

<% 

News noticias = new News();
List<News> lista = noticias.getNews("" ,5);

//ClienteRS cliente = new ClienteRS(); //  ESTA CLASE NO ESTA INCLUIDA AQUI
      int size= lista.size();
      
      int currentPage = 0; 
      int currentRow=0;
      int begin = 0;
      int previous =0;
      int next =0;
      int end = 0;
      int previousPage=0;
      int nextPage =0;
      int beginPage =1;
      int endPage=0;
      final int Rowsperpage = 2;
      int pages = lista.size() / Rowsperpage ;
    
    String requestCurrentRow = request.getParameter("currentRow");
    String requestCurrentPage = request.getParameter("currentPage"); 
      if ((requestCurrentRow == null  ||  requestCurrentPage == null ))
    	//if(lista.size()>0)
        {
           
           // size =  101; // ejemplo.. 
            session.setAttribute("size",new Integer(size));
            //pages = (size / Rowsperpage);
            if ( (size % Rowsperpage) > 0 ) pages++;
            session.setAttribute("pages", new Integer(pages));
            
             currentPage = 1; 
             currentRow=0;
             previous =0;
             if (size>10) {next =Rowsperpage;} else {next=0;}
             end = (pages - 1) * Rowsperpage;
             previousPage=1;
             if (size>Rowsperpage) { nextPage=2; } else { nextPage=1; }
        }
     else
        {
    	 	if (session.getAttribute("size") == null) 
    	 		size = lista.size(); //1 ; 
            else
                size = ((Integer)session.getAttribute("size")).intValue();
             
    	 	if (session.getAttribute("pages")==null) 
    	 		pages =  lista.size() % Rowsperpage ;
    	 	else
    	 	    pages = ((Integer)session.getAttribute("pages")).intValue();
    	 	
             currentPage = Integer.parseInt((String)request.getParameter("currentPage"));
             currentRow = Integer.parseInt((String)request.getParameter("currentRow"));
             
            if (currentPage > 1) 
             { 
               previous = (currentPage - 2) * Rowsperpage; 
             } 
            else { 
            	previous=0;
            }
               
            if (currentPage < pages) 
            { 
               next = currentRow + Rowsperpage; 
            }
            else
            { 
               next = (pages - 1) * Rowsperpage; 
            }
               
            end = (pages - 1) * Rowsperpage;
              
            if ( currentPage > 2)
            {
            	previousPage = currentPage - 1;
            	} 
            else
            {
            	previousPage = 1;
            }
            if ( currentPage < pages) 
            {
            	nextPage = currentPage + 1;
            }
            else {
            	nextPage=pages;
            	}
       }
		%>
    <% try { 
    
     if ( currentPage + 4 <= pages )
     {
          endPage = currentPage + 4;
          
          if  ( currentPage - 5 > 0 )
          beginPage= currentPage - 5;
          else
           { 
          beginPage = 1;
           if ( Rowsperpage > endPage ) endPage=Rowsperpage;
           if ( Rowsperpage > pages )   endPage=pages;
           }  
      
     }
      else
          {
          endPage = pages;
          if ( pages - Rowsperpage - 1 > 0  ) beginPage = pages - Rowsperpage - 1;
          }
      
      
    
    %>
<table width="208" border="0" cellspacing="0">
  <tr>
    <td>SIZE</td>
    <td><%=size%></td>
  </tr>
  <tr>
    <td>PAGES</td>
    <td><%=pages%></td>
  </tr>
  <tr>
    <td>CURRENTPAGE</td>
    <td><%=currentPage%></td>
  </tr>
  <tr>
    <td>CURRENTROW</td>
    <td><%=currentRow%></td>
  </tr>
  <tr>
    <td>BEGIN</td>
    <td><%=begin%></td>
  </tr>
  <tr>
    <td>PREVIOUS</td>
    <td><%=previous%></td>
  </tr>
  <tr>
    <td>NEXT</td>
    <td><%=next%></td>
  </tr>
  <tr>
    <td>END</td>
    <td><%=end%></td>
  </tr>
  <tr>
    <td>PREVIOUSPAGE</td>
    <td><%=previousPage%></td>
  </tr>
  <tr>
    <td>NEXTPAGE</td>
    <td><%=nextPage%></td>
  </tr>
  <tr>
    <td>BEGINPAGE</td>
    <td><%=beginPage%></td>
  </tr>
  <tr>
    <td>ENDPAGE</td>
    <td><%=endPage%></td>
  </tr>
</table><%} 
    catch (Exception e) {
    	
    	
    	
    }
    
    
 %>
 
 <% 
 
 if (pages>1) {
     
     
           
 %>
 <table width="650" border="0" cellspacing="0">
   <tr>
     <td width="75"><div align="center"><strong><a href="paginacion.jsp?currentRow=<%=begin%>&currentPage=1">&lt;&lt;</a></strong></div></td>
     <td width="245"><div align="center"><strong><a href="paginacion.jsp?currentRow=<%=previous%>&currentPage=<%=previousPage%>">&lt;</a></strong></div></td>
     <td width="102">&nbsp;</td>
     <td width="113"><div align="center"><strong><a href="paginacion.jsp?currentRow=<%=next%>&currentPage=<%=nextPage%>">&gt;</a></strong></div></td>
     <td width="105"><div align="center"><strong><a href="paginacion.jsp?currentRow=<%=end%>&currentPage=<%=pages%>">&gt;&gt;</a></strong></div></td>
   </tr>
   <tr>
     <td colspan="5"><div align="center">
     <%for (int i=beginPage; i<=endPage; i++) {%> 
     	<a href="paginacion.jsp?currentRow=<%=(i-1)*10%>&currentPage=<%=i%>"><%=i%> 
     	<%}%></a></div></td>
   </tr>
 </table>
			 <%
			 int intContador = 1 ;
        	 int intMinimo = ( Rowsperpage * (currentPage -1))  ;
        	 if (intMinimo == 0) intMinimo=1;
        	 
		     int intMaximo = (Rowsperpage * currentPage) -1 ;
			 for (News n : lista){
 	
 				String id = "";
				String body = "";
				String tittle = "";
				String author = "";
				String date = "";
		
				id = n.getIdNew().toString();
				tittle = n.getTittle();
				date = n.getDate();
				author = n.getAuthor();
				body = " " ; //n.getBody();
				
				if (intContador >= beginPage  && intContador <= endPage) {			
							%>
								<div class="article">
								<form name="f">
								<table border='0' cellpadding="0" align="center" class="blog" width="100%">
									<tr>
										<td><b><%=id%> </b> numero de registro <%=intContador %> </td>
									</tr>
									<tr>
										<td><b><%=tittle%></b></td>
									</tr>
									<tr>
										<td><b><%=date%></b></td>
									</tr>
									<tr>
										<td><i><%=author%></i></td>
									</tr>
									<tr>
										<td><%=body%></td>
									</tr>
								</table>
								</form>
								</div>
				<%				
								}
				intContador ++;
			 }
				%>
 
 <% 
 
 }
 
 
 %>
</body>
</html>
